var nodeRef = args.nodeRef;

var treeWalk = function (node) {
    
    // Base case: void or null node
    var jsonResult = 
        {
            "nodeRef": nodeRef,
            "treeSize": 0,
            "fileNum": 0,
            "folderNum": 0
        };
                  
    var children = node.getChildren ();

    if (children.length > 0 ) 
    {

        for (var i=0; i<children.length; i++) 
        {
            var child = children[i];
            var cname = child.name;
            if ( child.isContainer ) 
            {
                // it's a folder!
                // count the current folder
                jsonResult.folderNum ++;

                // call recursively the treeWalk function
                partialJsonResult = treeWalk (child);

                // update the result
                jsonResult.treeSize += partialJsonResult.treeSize;
                jsonResult.fileNum += partialJsonResult.fileNum;
                jsonResult.folderNum += partialJsonResult.folderNum;
            }
            else if (child.isDocument && cname !== 'doclib' && cname !== 'imgpreview' && cname !== 'webpreview' && cname !== 'Thumbs.db') 
            {
                // it's a file

                // count the current file
                jsonResult.fileNum ++;

                // update the global size
                jsonResult.treeSize += child.size;
            }
        }
    }
    
    return jsonResult;
};

// Execute script
var json = {};
if (nodeRef !== null) json = treeWalk (search.findNode (nodeRef));

model.json = jsonUtils.toJSONString(json);
